$(document).ready(function () {
    validateUser();
});


var ipAddress="localhost";
//验证用户是否登录
var validateUser=function(){
    $.ajax({
        //url: "http://" + ipAddress + ":8080/user/get_user_info",
        url: "http://"+ipAddress+":8004/orderInfo/checkAccount",
        type: "GET",
        async: true,
        data: null,
        contentType: "application/json",
        dataType: "json",
        success: function (data) {
            console.log(" data====  " + data);
            if (data.message =="success") {
                getOrderPayInfo();
            }else{
                layer.msg("请先登录！");
                setTimeout(function(){//1秒后跳转
                    window.location.href = "./login.html"
                },1000);
            }
        },
        error: function () {
            layer.msg("后台服务器开小差了...")
        }
    })
};

//加载页面信息
var getOrderPayInfo=function(){
    var orderNo=getUrlParms("orderNo");
    if (orderNo==""||orderNo==null){
        layer.msg("该订单不存在！");
        setTimeout(function(){//1秒后跳转
            window.location.href = "./index.html";
            //layer.msg("ddddddddd");
        },2000);
    }
    $.ajax({
        //url: "http://" + ipAddress + ":8080/user/get_user_info",
        url: "http://" + ipAddress + ":8004/orderInfo/queryByOrderNo/"+orderNo,
        type: "GET",
        async: true,
        data: null,
        contentType: "application/json",
        dataType: "json",
        success: function (data) {
            console.log(" data====  " + data);
            if (data.success ==true) {


                //设置订单号
                $("#orderNo").html(data.data.orderInfo.orderNo)

                var orderTime;
                if(data.data.orderInfo.orderTime!=null){
                    orderTime=data.data.orderInfo.orderTime;
                }

                $("#orderTime").html(orderTime);
                var payTip="";
                var status="";

                //金额
                var payment=0.0;
                //支付方式
                var payWay="";

                var payTime;

                if (data.data.orderInfo.payWay!=null) {
                    if(data.data.orderInfo.payWay==0){
                        payWay="支付宝";
                    }else{
                        payWay="其他支付方式";
                    }
                }


                if (data.data.orderInfo.payTime!=null){
                    payTime=data.data.orderInfo.payTime;
                    //layer.msg("payTime==="+payTime);
                    $("#payInfoTable").append("<tr>\n" +
                        "                                            <th>支付时间：</th>\n" +
                        "                                            <td id='payTime'>"+payTime+"</td>\n" +
                        "                                        </tr>")
                }



                var operation1="<tr>" +
                    "<td class=\"order-actions\" colspan='3'>" +
                        "<a class=\"btn btn-small btn-primary\"\n href='#' data-id='"+data.data.orderInfo.orderNo+"'>打印订单</a>\n" +
                        "<button class=\"btn btn-small btn-primary\"\n data-id='"+data.data.orderInfo.orderNo+"' onclick='cancelOrder(this)'>取消订单</button>\n" +
                    "</td>"+
                    "</tr>\n"+
                    "<tr>" +
                    "<td class=\"order-actions\" colspan='3'>" +
                    "<a class=\"btn btn-small btn-primary\"\n href='#' target=\"_blank\" data-id='"+data.data.orderInfo.orderNo+"' onclick='pay(this)'>立即支付</a>\n" +
                    "</td>"+
                    "</tr>\n";

                var operation2="<tr>" +
                    "<td class=\"order-actions\" colspan='3'>" +
                    "<a class=\"btn btn-small btn-primary\"\n href='#' data-id='"+data.data.orderInfo.orderNo+"'>打印订单</a>\n" +
                    "<a class=\"btn btn-small btn-primary\"\n href='#' data-id='"+data.data.orderInfo.orderNo+"' onclick='applyRefund(this)'>申请退款</a>\n" +
                    "</td>"+
                    "</tr>\n";

                var operation3="<tr>" +
                    "<td class=\"order-actions\" colspan='3'>" +
                    "<a class=\"btn btn-small btn-primary\"\n href='#' data-id='"+data.data.orderInfo.orderNo+"'>打印订单</a>\n" +
                    "</td>"+
                    "</tr>\n";


                var operation;

                //设置商品信息
                switch (data.data.orderInfo.orderStatus) {
                    case 0:
                        status="待付款";
                        payTip="应付金额：";
                        payment=data.data.orderInfo.totalMerchandise + data.data.orderInfo.transferFee;
                        payWay="该订单未支付！";
                        operation=operation1;
                        break;
                    case 1:
                        status="待发货";
                        payTip="订单总额：";
                        payment=data.data.orderInfo.actualPayment;
                        operation=operation2;
                        break;
                    case 2:
                        status="待收货";
                        payTip="订单总额：";
                        payment=data.data.orderInfo.actualPayment;
                        operation=operation2;
                        break;
                    case 3:
                        status="待评价";
                        payTip="订单总额：";
                        payment=data.data.orderInfo.actualPayment;
                        if(data.data.orderInfo.orderTime!=null){
                            orderTime=data.data.orderInfo.orderTime;
                        }
                        operation=operation2;
                        break;
                    case 4:
                        status="已完成";
                        payTip="订单总额：";
                        payment=data.data.orderInfo.actualPayment;
                        break;
                    case 5:
                        status="已关闭";
                        payTip="订单总额：";
                        payment=data.data.orderInfo.totalMerchandise + data.data.orderInfo.transferFee;
                        payWay="该订单未支付！";
                        operation=operation3;
                        break;
                    default:
                        status="未知类型";
                }


                $.each(data.data.orderProductVOTwoList,function (index,value) {
                    $("#orderItem").append("<tr>\n" +
                        "                                            <td class=\"col col-thumb\">\n" +
                        "                                                <div class=\"figure figure-thumb\">\n" +
                        "                                                    <a target=\"_blank\" href=\"//item.mi.com/1171600001.html\">\n" +
                        "                                                        <img id='goodsPicture"+index+"' src='"+value.goodsVO.picture+"' width=\"80\" height=\"80\" alt=\"\" />\n" +
                        "                                                    </a>\n" +
                        "                                                </div>\n" +
                        "                                            </td>\n" +
                        "                                            <td class=\"col col-name\">\n" +
                        "                                                <p class=\"name\">\n" +
                        "                                                    <a target=\"_blank\" href='#' id='goodsName"+index+"'>"+value.orderProduct.goodsName+"</a>\n" +
                        "                                                </p>\n" +
                        "                                            </td>\n" +
                        "                                            <td class=\"col col-price\">\n" +
                        "\n" +
                        "                                                <p class=\"price\"><span id='goodsPrice"+index+"'>"+value.orderProduct.goodsPrice+"</span>元 &times; <span id='goodsNumber"+index+"'>"+value.orderProduct.goodsNumber+"</span></p>\n" +
                        "\n" +
                        "                                            </td>\n" +
                        "                                            <td class=\"col col-actions\">\n" +
                        "                                            </td>\n" +
                        "                                        </tr>")
                });

                //设置订单状态
                $("#orderStatus").html(status);

                //设置商品图片
                $("#goodsPicture").attr("src",data.data.orderProductVOTwoList[0].goodsVO.picture);

                //设置商品名
                $("#goodsName").html(data.data.orderProductVOTwoList[0].goodsVO.skuName);

                //设置商品价格
                console.log("goodsPrice===="+data.data.orderProductVOTwoList[0].orderProduct.goodsPrice);
                $("#goodsPrice").html(data.data.orderProductVOTwoList[0].orderProduct.goodsPrice);

                //设置购买数量
                console.log("goodsNumber===="+data.data.orderProductVOTwoList[0].orderProduct.goodsNumber);
                $("#goodsNumber").html(data.data.orderProductVOTwoList[0].orderProduct.goodsNumber);

                //设置收货人姓名
                $("#receiptName").html(data.data.receipt.name);

                //设置收货人联系电话
                $("#receiptPhone").html(data.data.receipt.phone);

                //设置收货人地址
                $("#receiptAddress").html(data.data.receipt.address);

                //设置支付方式
                console.log("payWay=="+payWay);
                $("#payWay").html(payWay);

                //设置订单备注
                $("#remark").html(data.data.orderInfo.remark);

                //设置商品总额
                $("#totalAmount").html(data.data.orderInfo.totalMerchandise);

                //设置运费
                $("#transferFee").html(data.data.orderInfo.transferFee);

                //设置金额提示
                $("#payTip").html(payTip);

                //设置金额
                $("#payment").html(payment);

                //去付款按钮
                $("#orderCount").append(operation);


            }else{
                if (data.message=="请先登录！") {
                    layer.msg("请先登录！");
                    setTimeout(function(){//1秒后跳转
                        window.location.href = "./login.html"
                    },1000);
                }else{
                    layer.msg(""+data.message);
                }

            }
        },
        error: function () {
            layer.msg("后台服务器开小差了...")
        }
    })

};

//去付款
var pay=function (obj) {

};

//取消订单
var cancelOrder=function(obj){

    layer.confirm('确定取消订单吗？', {
        btn: ['确定', '取消']
    }, function () {

        var orderNo=$(obj).attr("data-id");
        //alert("orderNo==="+orderNo);

        // var orderNo=getUrlParms("orderNo");
        var orderInfo={
            orderNo:orderNo,
            orderStatus:5
        };
        console.log("orderInfo=====gggggg========="+orderInfo);
        $.ajax({
            url: "http://"+ipAddress+":8004/orderInfo/updateStatusByOrderNo",
            type: "PUT",
            async: true,
            data: JSON.stringify(orderInfo),
            contentType: "application/json",
            dataType: "json",
            success: function (data) {
                console.log(" data====  " + data);
                if (data.success ==true) {
                    layer.msg("该订单已被取消！");
                    setTimeout(function(){//2秒后跳转
                        window.location.href = "./orderDetail.html?orderNo="+orderNo;
                    },2000);
                }else{
                    layer.msg("请先登录！");
                    setTimeout(function(){//1秒后跳转
                        window.location.href = "./login.html"
                    },1000);
                }
            },
            error: function () {
                layer.msg("后台服务器开小差了...")
            }
        });
    });

};

//申请退款
var applyRefund=function(obj){
    layer.confirm('确定申请退款吗？', {
        btn: ['确定', '取消']
    }, function () {
        layer.msg("您的申请已提交");
    });
};


















